﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.IO;
using System.Diagnostics;

namespace suidtool
{
    public partial class SuidEditor : Form
    {
        public String UserSuid { get { return uSuid; } }
        private static Regex stripHex = new Regex(@"(?:0x|[^a-fA-F0-9])");
        private string uSuid;
        private FileInfo mkvpe;

        public SuidEditor()
        {
            InitializeComponent();
        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            suidErrorProvider.Clear();
            base.OnFormClosing(e);
        }

        private void SetEnabledState(bool bEnable)
        {
            foreach (Control c in this.Controls)
            {
                c.Enabled = bEnable;
            }
        }

        public DialogResult Show(string filePath, string suid, FileInfo mkvpe)
        {
            lock (this)
            {
                this.mkvpe = mkvpe;

                fileText.Text = filePath;
                suidText.Text = suid;
                suidText.Select();

                return base.ShowDialog();
            }
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            string suid = stripHex.Replace(suidText.Text, "");
            suidText.Text = suid;

            if (suid.Length != 32)
            {
                suidErrorProvider.SetError(suidText, "Enter a valid Segment UID (128-bit hex)");
            }
            else if (!backgroundUIDChanger.IsBusy)
            {
                SetEnabledState(false);
                
                var info = new Tuple<FileInfo, string, string>(mkvpe, fileText.Text, suid);
                backgroundUIDChanger.RunWorkerAsync(info);
            }
        }

        private void backgroundUIDChanger_DoWork(object sender, DoWorkEventArgs e)
        {
            var info = e.Argument as Tuple<FileInfo, string, string>;
            e.Result = "";

            using (Process p = new Process())
            {
                p.StartInfo.FileName = info.Item1.FullName;
                p.StartInfo.Arguments = "-e info -s \"segment-uid=" + info.Item3 +
                                        "\" \"" + info.Item2 + "\"";
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.CreateNoWindow = true;
                
                p.Start();
                p.WaitForExit();
                if (p.ExitCode == 0)
                {
                    e.Result = info.Item3;
                }
            }
        }

        private void backgroundUIDChanger_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            SetEnabledState(true);
            uSuid = e.Result as string;

            if (uSuid.Length > 0)
            {
                this.DialogResult = DialogResult.OK;
                Close();
            }
            else
            {
                suidErrorProvider.SetError(suidText, "Failed to set Segment UID (Unknown reason)");
            }            
        }
    }
}
